﻿using iMES.Mass.IRepositories;
using iMES.Mass.IServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Threading.Tasks;

namespace iMES.Mass.Controllers
{
    public partial class Mass_DetectionTemplate_ItemController
    {
        private readonly IMass_DetectionTemplate_ItemService _ItemService;//检测模版-检测项信息
        private readonly IMass_DetectionTemplate_ItemRepository _templateTestItem;//检测模版-检测项信息
        private readonly IMass_DetectionTemplate_ProductRepository _templateProduct;//检测模版-产品
        private readonly IMass_DetectionTemplateRepository _template;//检测模版
        private readonly IHttpContextAccessor _httpContextAccessor;
        [ActivatorUtilitiesConstructor]
        public Mass_DetectionTemplate_ItemController(
            IMass_DetectionTemplate_ItemService itemService,
            IMass_DetectionTemplate_ItemRepository templateTestItem,
            IMass_DetectionTemplate_ProductRepository templateProduct,
            IMass_DetectionTemplateRepository template,
            IHttpContextAccessor httpContextAccessor) : base(itemService)
        {
            _templateTestItem = templateTestItem;
            _templateProduct = templateProduct;
            _template = template;
            _httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 来料检验单根据产品主键获取测试项
        /// </summary>
        /// <param name="ProductId">产品主键</param>
        /// <returns></returns>
        [Route("getTestItemRows"), HttpGet]
        public async Task<IActionResult> GetTestItemRows(int ProductId)
        {
            //检测模版-产品
            int templateId = _templateProduct.FindAsIQueryable
                (x => x.Mass_DetectionTemplate_Product_Product_ID == ProductId)
                .OrderByDescending(x => x.CreateDate)
                .Select(s => s.Mass_DetectionTemplate_Product_Template_ID).FirstOrDefault();

            //检测模版
            templateId = _template.FindAsIQueryable
                (x => x.Mass_DetectionTemplate_ID == templateId && x.Mass_DetectionTemplate_Type.Contains("LLJY") && x.Mass_DetectionTemplate_IsNo == 1)
                .OrderByDescending(x => x.CreateDate)
                .Select(s => s.Mass_DetectionTemplate_ID).FirstOrDefault();

            if (templateId != 0)
            {
                //检测模版-检测项信息
                var rows = await _templateTestItem.FindAsIQueryable(x => x.Mass_DetectionTemplate_Item_TemplateId == templateId).ToListAsync();
                //获取当前库存数量
                return JsonNormal(rows);
            }
            else
            {
                return JsonNormal("[]");
            }
        }
        /// <summary>
        /// 出货检验单根据产品主键获取测试项
        /// </summary>
        /// <param name="SalesOrder_Id">产品主键</param>
        /// <returns></returns>
        [Route("getOutCheckTestItemRows"), HttpGet]
        public async Task<IActionResult> GetOutCheckTestItemRows(int ProductId)
        {
            //检测模版-产品
            int templateId = _templateProduct.FindAsIQueryable
                (x => x.Mass_DetectionTemplate_Product_Product_ID == ProductId)
                .OrderByDescending(x => x.CreateDate)
                .Select(s => s.Mass_DetectionTemplate_Product_Template_ID).FirstOrDefault();

            //检测模版
            templateId = _template.FindAsIQueryable
                (x => x.Mass_DetectionTemplate_ID == templateId && x.Mass_DetectionTemplate_Type.Contains("FHJY") && x.Mass_DetectionTemplate_IsNo == 1)
                .OrderByDescending(x => x.CreateDate)
                .Select(s => s.Mass_DetectionTemplate_ID).FirstOrDefault();

            if (templateId != 0)
            {
                //检测模版-检测项信息
                var rows = await _templateTestItem.FindAsIQueryable(x => x.Mass_DetectionTemplate_Item_TemplateId == templateId).ToListAsync();
                //获取当前库存数量
                return JsonNormal(rows);
            }
            else
            {
                return JsonNormal("[]");
            }
        }
        /// <summary>
        /// 过程检验单根据产品主键获取测试项
        /// </summary>
        /// <param name="SalesOrder_Id">产品主键</param>
        /// <returns></returns>
        [Route("getProcessTestItemRows"), HttpGet]
        public async Task<IActionResult> GetProcessTestItemRows(int ProductId)
        {
            //检测模版-产品
            int templateId = _templateProduct.FindAsIQueryable
                (x => x.Mass_DetectionTemplate_Product_Product_ID == ProductId)
                .OrderByDescending(x => x.CreateDate)
                .Select(s => s.Mass_DetectionTemplate_Product_Template_ID).FirstOrDefault();


            templateId = _template.FindAsIQueryable
                (x => x.Mass_DetectionTemplate_ID == templateId && (x.Mass_DetectionTemplate_Type.Contains("CPJY") || x.Mass_DetectionTemplate_Type.Contains("SJ") || x.Mass_DetectionTemplate_Type.Contains("XJ") || x.Mass_DetectionTemplate_Type.Contains("MJ")) && x.Mass_DetectionTemplate_IsNo == 1)
                .OrderByDescending(x => x.CreateDate)
                .Select(s => s.Mass_DetectionTemplate_ID)
                .FirstOrDefault();

            if (templateId != 0)
            {
                //检测模版-检测项信息
                var rows = await _templateTestItem.FindAsIQueryable(x => x.Mass_DetectionTemplate_Item_TemplateId == templateId).ToListAsync();
                //获取当前库存数量
                return JsonNormal(rows);
            }
            else
            {
                return JsonNormal("[]");
            }
        }
    }
}
